﻿using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Diagnostics.Contracts;
using System.Threading;
using System.Transactions;

namespace O1.Kernel.Storage.Transactions
{
    [ContractVerification(true)]
    public struct MultiVersionScope
    {
        private readonly MultiVersionScope outerScope;

        private readonly bool hasOuterScope;

        public MultiVersionScope(long transactionId)
            : this()
        {
            this.TransactionId = transactionId;
        }

        public MultiVersionScope(long transactionId, MultiVersionScope outerScope)
            : this()
        {
            this.outerScope = outerScope;
            this.TransactionId = transactionId;
            this.hasOuterScope = true;
        }

        public long TransactionId { get; private set; }

        public bool InScope(long transactionId)
        {
            var outer = this;
            var hasScope = true;
            while (hasScope)
            {
                if (outer.TransactionId == transactionId)
                {
                    return true;
                }

                outer = outer.outerScope;
                hasScope = outer.hasOuterScope;
            }

            return false;
        }
    }
}
